/**
 * @filename: MyGenerator.java 2019-10-16
 * @project v0.0.1  V1.0
 * Copyright(c) 2018 BianPeng Co. Ltd. 
 * All right reserved. 
 */
package com.zm.mybatis.gen;

import com.zm.mybatis.gen.entity.ServerBasisInfo;
import com.zm.mybatis.gen.generator.JavaClipBoardGenerator;
import com.zm.mybatis.gen.generator.PageVueGenerator;
import com.zm.mybatis.gen.generator.ServerJavaGenerator;
import com.zm.mybatis.gen.util.EntityInfoUtil;
import com.zm.mybatis.gen.util.FreemarkerUtil;
import com.zm.mybatis.gen.util.MySqlToJavaUtil;
import lombok.Setter;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class GeneratorManager {

	public GeneratorManager(String database, String url, String userName, String pwd) {
		this.database = database;
		this.url = url;
		this.userName = userName;
		this.pwd = pwd;
	}

	public String database;
	public String url;
	public String userName;
	public String pwd;



	@Setter
	private String entityUrl;
	@Setter
	private String entityResponseUrl;
	@Setter
	private String entityRequestUrl;
	@Setter
	private String mapperUrl;
	@Setter
	private String serviceUrl;
	@Setter
	private String serviceImplUrl;
	@Setter
	private String controllerUrl;
	@Setter
	private String xmlUrl;
	@Setter
	private String mapperTestUrl;
	@Setter
	private String serviceTestUrl;


	public String CLASSCOMMENT = "API应用KEY";
	public String TIME = new Date().getYear() + "-" + new Date().getMonth() + "-" + new Date().getDate();
	public String AGILE = new Date().getTime() + "";
	// 路径信息，分开路径方便聚合工程项目，微服务项目
	public String IS_SWAGGER = "true";
	// 基础信息：项目名、作者、版本
	public String PROJECT = "project";
	public String AUTHOR = "test";
	public String VERSION = "V1.0";

	public static String CONTROLLER = "CONTROLLER";
	public static String SERVICE = "SERVICE";

	public void genCodes(String serverFileUrl, String pageFileUrl, String tableName, String subPrefix, List<String> genCodeStrategy) {
		ServerBasisInfo bi = new ServerBasisInfo(PROJECT, AUTHOR, VERSION, url, userName, pwd, database, TIME, AGILE,
				entityUrl, entityResponseUrl, entityRequestUrl,
				mapperUrl, xmlUrl, serviceUrl, serviceImplUrl, controllerUrl,IS_SWAGGER);
		bi.setTable(tableName);

		String fixedTableName = tableName.replace(subPrefix, "");
		bi.setEntityName(MySqlToJavaUtil.getClassName(fixedTableName));
		bi.setEntityRequestName(bi.getEntityName()+ "Request");
		bi.setEntityResponseName(bi.getEntityName()+ "Response");
		bi.setMapperTestName(bi.getEntityName()+ "MapperTest");
		bi.setServiceTestName(bi.getEntityName()+ "ServiceTest");
		bi.setDaoClassNameSuffix("Mapper");
		bi.setObjectName(MySqlToJavaUtil.changeToControllerPath(fixedTableName));
		bi.setEntityComment(CLASSCOMMENT);
		bi.setMapperTestUrl(mapperTestUrl);
		bi.setServiceTestUrl(serviceTestUrl);
		try {
			bi = EntityInfoUtil.readDB(bi);

			// 设置模板路径
			FreemarkerUtil javaTemplate = new FreemarkerUtil();
			javaTemplate.setTemplatePath("/crmftl/server");
			ServerJavaGenerator javaGenerator = new ServerJavaGenerator();
			javaGenerator.setFreemarkerUtil(javaTemplate);
			//开始生成文件

			// mapper 测试类
			String aa7 = javaGenerator.createMapperTest(serverFileUrl, bi).toString();


			String aa1 = javaGenerator.createEntity(serverFileUrl, bi).toString();
			String aa2 = javaGenerator.createDao(serverFileUrl, bi).toString();
			String aa3 = javaGenerator.createDaoImpl(serverFileUrl, bi).toString();

			System.out.println(aa1);
			System.out.println(aa2);
			System.out.println(aa3);


			if(genCodeStrategy.contains(SERVICE)) {
				String aa4 = javaGenerator.createService(serverFileUrl, bi).toString();
				String aa5 = javaGenerator.createServiceImpl(serverFileUrl, bi).toString();
				String aa8 = javaGenerator.createServiceTest(serverFileUrl, bi).toString();
				System.out.println(aa4);
				System.out.println(aa5);
				System.out.println(aa8);
			}
			if(genCodeStrategy.contains(CONTROLLER)) {
				String aa11 = javaGenerator.createEntityResponse(serverFileUrl, bi).toString();
				String aa111 = javaGenerator.createEntityRequest(serverFileUrl, bi).toString();
				String aa6 = javaGenerator.createController(serverFileUrl, bi).toString();
				System.out.println(aa11);
				System.out.println(aa111);
				System.out.println(aa6);
			}



			PageVueGenerator pageVueGenerator = new PageVueGenerator();
			// 设置前端模板路径
			FreemarkerUtil pageTemplate = new FreemarkerUtil();
			pageTemplate.setTemplatePath("/crmftl/page");
			pageVueGenerator.setFreemarkerUtil(pageTemplate);
			pageVueGenerator.createTemplate(pageFileUrl, bi);

		} catch (SQLException e) {
			e.printStackTrace();
		}
	}


	public void genCodesSetToClipBoard(String serverFileUrl, String tableName, String subPrefix) {
		ServerBasisInfo bi = new ServerBasisInfo(PROJECT, AUTHOR, VERSION, url, userName, pwd, database, TIME, AGILE,
				entityUrl, entityResponseUrl, entityRequestUrl,
				mapperUrl, xmlUrl, serviceUrl, serviceImplUrl, controllerUrl,IS_SWAGGER);
		bi.setTable(tableName);

		String fixedTableName = tableName.replace(subPrefix, "");
		bi.setEntityName(MySqlToJavaUtil.getClassName(fixedTableName));
		bi.setEntityRequestName(bi.getEntityName()+ "Request");
		bi.setEntityResponseName(bi.getEntityName()+ "Response");
		bi.setMapperTestName(bi.getEntityName()+ "MapperTest");
		bi.setServiceTestName(bi.getEntityName()+ "ServiceTest");
		bi.setDaoClassNameSuffix("Mapper");
		bi.setObjectName(MySqlToJavaUtil.changeToControllerPath(fixedTableName));
		bi.setEntityComment(CLASSCOMMENT);
		bi.setMapperTestUrl(mapperTestUrl);
		bi.setServiceTestUrl(serviceTestUrl);
		try {
			bi = EntityInfoUtil.readDB(bi);

			// 设置模板路径
			FreemarkerUtil javaTemplate = new FreemarkerUtil();
			javaTemplate.setTemplatePath("/crmftl/server");
			JavaClipBoardGenerator javaGenerator = new JavaClipBoardGenerator();
			javaGenerator.setFreemarkerUtil(javaTemplate);
			//开始生成文件
			String aa1 = javaGenerator.createEntity(serverFileUrl, bi).toString();
			System.out.println(aa1);

		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
